package com.neusoft.Shixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.Shixun.dao.CareLevelDao;
import com.neusoft.Shixun.dao.ClientCareItemDao;
import com.neusoft.Shixun.dao.ClientCareSettingDao;
import com.neusoft.Shixun.po.CareLevel;
import com.neusoft.Shixun.po.ClientCareItem;
import com.neusoft.Shixun.po.ClientCareSetting;
import com.neusoft.Shixun.po.ResponseBean;
import com.neusoft.Shixun.service.ClientCareSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientCareSettingServiceImpl implements ClientCareSettingService {

    @Autowired
    private ClientCareSettingDao clientCareSettingDao;

    @Autowired
    private CareLevelDao careLevelDao;

    @Autowired
    private ClientCareItemDao clientCareItemDao;

    @Override
    public ResponseBean<List<ClientCareSetting>> getAllClientCareSettings() {
        List<ClientCareSetting> settings = clientCareSettingDao.selectList(null);
        return new ResponseBean<>(settings);
    }

    @Override
    public ResponseBean<ClientCareSetting> getClientCareSettingById(Integer id) {
        ClientCareSetting setting = clientCareSettingDao.selectById(id);
        if (setting != null) {
            return new ResponseBean<>(setting);
        } else {
            return new ResponseBean<>(404, "客户护理设置未找到");
        }
    }

    @Override
//    添加客户护理级别
    public ResponseBean<Integer> addClientCareSetting(ClientCareSetting setting) {
        int result = clientCareSettingDao.insert(setting);
        return new ResponseBean<>(result);
    }

    @Override
//    修改客户护理级别，实际上传入的是客户id和级别id
    public ResponseBean<Integer> updateClientCareSetting(ClientCareSetting setting) {
        ClientCareSetting clientCareSetting = clientCareSettingDao.selectOne(new QueryWrapper<ClientCareSetting>().eq("client_id", setting.getClientId()));
        if (clientCareSetting != null) {
            clientCareSetting.setLevelId(setting.getLevelId());
        }
        int result = clientCareSettingDao.updateById(clientCareSetting);
        if (result > 0) {
            return new ResponseBean<>(result); // 返回更新成功的行数
        } else {
            return new ResponseBean<>(500, "客户护理级别更新失败");
        }
    }

    @Override
//  移除客户的护理级别，同时级联删除客户的护理项目
    public ResponseBean<Integer> deleteClientCareSetting(Integer id) {
        List<ClientCareItem> clientCareItems = clientCareItemDao.selectList(new QueryWrapper<ClientCareItem>().eq("client_id", id));
        if (!clientCareItems.isEmpty()) {
            clientCareItemDao.deleteBatchIds(clientCareItems);
        }
        ClientCareSetting clientCareSetting = clientCareSettingDao.selectOne(new QueryWrapper<ClientCareSetting>().eq("client_id", id));
        int result = clientCareSettingDao.deleteById(clientCareSetting);
        return new ResponseBean<>(result);
    }

    @Override
//    根据客户id查询客户的护理级别信息
    public ResponseBean<CareLevel> getClientCareSettingByClientId(Integer id) {
        ClientCareSetting result = clientCareSettingDao.selectOne(new QueryWrapper<ClientCareSetting>().eq("client_id", id));
        if (result == null) {
            return null;
        }
        CareLevel careLevel = careLevelDao.selectById(result.getLevelId());
        return new ResponseBean<>(careLevel);
    }
}